package it.antonio.ilpizze.swing.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;

public class Swings {

	public static void center(Window w) {
		Dimension us = w.getSize();
		Dimension them = Toolkit.getDefaultToolkit().getScreenSize();
		int newX = (them.width - us.width) / 2;
		int newY = (them.height - us.height) / 2;
		w.setLocation(newX, newY);
	}

	public static void maximize(Window w) {
		Dimension them = Toolkit.getDefaultToolkit().getScreenSize();
		w.setBounds(0, 0, them.width, them.height);
	}

	public static void centerOnParent(Component w) {
		Dimension parent = w.getParent().getSize();
		Dimension window = w.getSize();
		int xCoord = w.getParent().getLocationOnScreen().x
				+ (parent.width / 2 - window.width / 2);
		int yCoord = w.getParent().getLocationOnScreen().y
				+ (parent.height / 2 - window.height / 2);

		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int xOffScreenExcess = xCoord + window.width - screen.width;
		if (xOffScreenExcess > 0) {
			xCoord = xCoord - xOffScreenExcess;
		}
		if (xCoord < 0) {
			xCoord = 0;
		}
		int yOffScreenExcess = yCoord + window.height - screen.height;
		if (yOffScreenExcess > 0) {
			yCoord = yCoord - yOffScreenExcess;
		}
		if (yCoord < 0) {
			yCoord = 0;
		}

		w.setLocation(xCoord, yCoord);
	}
}